﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FortuneHeaven.Web.Common;
using System.Text;
using FortuneHeavenBAL;
using FortuneHeavenCommon.DataMap;
using FortuneHeavenCommon.Enum;

namespace FortuneHeaven.Web.Pages.Shop
{
    public partial class EquipPurchase : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SetPageHeader(FortuneHeavenCommon.Enum.MainPageTabType.shop);
           
            StringBuilder builder = new StringBuilder();

            this.usPager.PagerChanged += new Web.Controls.SePagination.PagerChangeHandler(usPager_PagerChanged);
            uclogin.LoginSuccess += new Web.Controls.LoginPanel.LoginSuccessHandler(uclogin_LoginSuccess);
      
            if (!IsPostBack)
            {
                if (AuthorityCenter.CurrentUserID > 0)
                {
                    ShowUser();
                }
                else
                {
                    ShowLogin();
                }
                this.usPager.PageSize = 9;
                ResetList();
            }
        }

        void uclogin_LoginSuccess()
        {
            ShowUser();
        }

        private void ShowUser()
        {
            divHeadpic.Visible = true;
            uclogin.Visible = false;
            SetUserInfor();
        }
        private void ShowLogin()
        {
            divHeadpic.Visible = false;
            uclogin.Visible = true;
        }

        public void SetUserInfor()
        {
            ltUserName.Text = AuthorityCenter.CurrentUserName;
            UserInforBO bo = new UserInforBO();
            UserInfor infor = bo.GetUserInfor(AuthorityCenter.CurrentUserID);
            ltFortune.Text = infor.FortuneValue.ToString();
            if (!string.IsNullOrEmpty(infor.ShowImg))
            {
                imgHead.Src = GetPagesURL(FortuneHeavenCommon.ComMethod.CommonPath.GameShow, infor.ShowImg);
            }
        }

        void usPager_PagerChanged(object sender, int currentpage)
        {
            EquipmentBO gift = new EquipmentBO();
            this.rpEquip.DataSource = gift.GetEquips(this.usPager.CurrentPageIndex, this.usPager.PageSize);
            rpEquip.DataBind();
        }

        public void ResetList()
        {
            EquipmentBO gift = new EquipmentBO();
            this.usPager.RecordCount = gift.GetTotalSize();
            this.usPager.CurrentPageIndex = 1;
        }

        public void PurchaseHead(object sender, EventArgs e)
        {
            if (AuthorityCenter.CurrentUserID <= 0)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "infor", "alert('请先登陆!');", true);
                //Response.Write("<script> alert('请先登陆!');document.location ='" + Page.ResolveUrl("~/Login.aspx") + "'</script>");
            }
            else
            {
                LinkButton choose = (sender as LinkButton);
                string inputid = choose.CommandArgument;
                int headid = -1;
                int.TryParse(inputid, out headid);
                try
                {
                    if (headid >= 0)
                    {
                        UserInforBO bo = new UserInforBO();
                        UserInfor infor = bo.GetUserInfor(AuthorityCenter.CurrentUserID);
                        EquipmentBO equipbo = new EquipmentBO();
                        Equipment equip = equipbo.GetInfo(headid);
                        GameScoreLockerBO lockbo = new GameScoreLockerBO();
                        if (lockbo.GetLocker(AuthorityCenter.CurrentUserID) != null)
                        {
                            ShowCurError("您没有退出游戏不能购买物品！");
                            return;
                        }
                        if (infor != null)
                        {
                            if (infor.FortuneValue >= equip.Price)
                            {
                                infor.FortuneValue = infor.FortuneValue - equip.Price;
                                //infor.ShowImg = gameshow.Image;
                                bo.SaveUserInfor(infor);
                                //道具逻辑
                                if (equip.Type<=3)
                                {
                                    AccountsInfoBO acountbo = new AccountsInfoBO();
                                    AccountsInfo acount = acountbo.GetAcount(AuthorityCenter.CurrentUserID);
                                    acount.MemberOrder = equip.Type;
                                    if ((EquipmentType)equip.Type == EquipmentType.red)
                                    {
                                        if (acount.MemberOverDate != DateTime.MinValue)
                                        {
                                            acount.MemberOverDate = acount.MemberOverDate.AddMonths(1);
                                        }
                                        else
                                        {
                                            acount.MemberOverDate = DateTime.Now.AddMonths(1);
                                        }
                                    }
                                    if ((EquipmentType)equip.Type == EquipmentType.blue)
                                    {
                                        if (acount.MemberOverDate != DateTime.MinValue)
                                        {
                                            acount.MemberOverDate = acount.MemberOverDate.AddMonths(3);
                                        }
                                        else
                                        {
                                            acount.MemberOverDate = DateTime.Now.AddMonths(3);
                                        }
                                    }
                                    if ((EquipmentType)equip.Type == EquipmentType.yellow)
                                    {
                                        if (acount.MemberOverDate != DateTime.MinValue)
                                        {
                                            acount.MemberOverDate = acount.MemberOverDate.AddMonths(6);
                                        }
                                        else
                                        {
                                            acount.MemberOverDate = DateTime.Now.AddMonths(6);
                                        }
                                    }
                                    acountbo.Save(acount);
                                }
                                UserEquipmentListBO ordersbo = new UserEquipmentListBO();
                                UserEquipmentList order = new UserEquipmentList();
                                order.EquipID = equip.ID;
                                order.UserID = AuthorityCenter.CurrentUserID;
                                order.ID = ordersbo.GetMaxID();
                                order.Time = DateTime.Now;
                                ordersbo.Save(order,true);
                                Response.Redirect("PurchaseEquipSuccess.aspx");
                            }
                            else
                            {
                                ShowCurError("财富值不够！");
                            }
                        }
                        //GameShowOrdersBO game = new GameShowOrdersBO();

                    }
                }
                catch
                {
                    ShowCurError("服务器发生异常！");
                }
            }
        }
    }
}